//package com.crhms.seabow.controller.test;
//
//import com.crhms.seabow.entity.AtcDrgStat;
//import com.crhms.seabow.entity.Role;
//import com.fasterxml.jackson.core.JsonProcessingException;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
//import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
//import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
//import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
//import org.elasticsearch.action.get.GetRequest;
//import org.elasticsearch.action.get.GetResponse;
//import org.elasticsearch.action.index.IndexRequest;
//import org.elasticsearch.action.index.IndexResponse;
//import org.elasticsearch.action.search.SearchRequest;
//import org.elasticsearch.action.search.SearchResponse;
//import org.elasticsearch.action.support.master.AcknowledgedResponse;
//import org.elasticsearch.action.update.UpdateRequest;
//import org.elasticsearch.action.update.UpdateResponse;
//import org.elasticsearch.client.RequestOptions;
//import org.elasticsearch.client.RestHighLevelClient;
//import org.elasticsearch.common.settings.Settings;
//import org.elasticsearch.common.unit.TimeValue;
//import org.elasticsearch.common.xcontent.XContentBuilder;
//import org.elasticsearch.common.xcontent.XContentFactory;
//import org.elasticsearch.common.xcontent.XContentType;
//import org.elasticsearch.common.xcontent.json.JsonXContent;
//import org.elasticsearch.search.SearchHit;
//import org.elasticsearch.search.SearchHits;
//import org.elasticsearch.search.builder.SearchSourceBuilder;
//import org.elasticsearch.search.sort.SortOrder;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RestController;
//
//import java.io.IOException;
//import java.util.HashMap;
//import java.util.Map;
//
//import static org.elasticsearch.index.query.QueryBuilders.matchQuery;
//
//@RequestMapping("/es")
//@RestController
//public class EsController {
//
//    @Autowired
//    private ObjectMapper objectMapper;
//    private String index="atc_drg_stat";
//    private String type="man";
//    @Autowired
//    private RestHighLevelClient client;
//
//    @RequestMapping("/searchAfter")
//    public void searchAfter() throws IOException {
//        String searchAfter =null;
//        Object[] objects = new Object[]{};
//        try {
//            for (int i = 0; i < 100; i++) {
//                SearchRequest searchRequest = new SearchRequest(index);
//                SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//                searchSourceBuilder.query(matchQuery("year_month", "yearMonth"));
//                searchSourceBuilder.size(1000);
//                searchSourceBuilder.sort("sort_id", SortOrder.ASC);
//                if(objects.length>0) {
//                    searchSourceBuilder.searchAfter(objects);
//                }
//                searchRequest.source(searchSourceBuilder);
//                System.out.println(searchRequest.source().toString());
//                SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
//                System.out.println(searchResponse);
//                SearchHit[] hits = searchResponse.getHits().getHits();
//                objects = hits[hits.length-1].getSortValues();
//
//                System.out.println(hits);
//            }
//
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//
//    @RequestMapping("/get")
//    public void get() throws IOException {
//        try {
//            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//            SearchRequest rq = new SearchRequest();
//            //索引
//            rq.indices(index);
//
//            //起始位置
//            sourceBuilder.from(0);
//            //每页数量
//            sourceBuilder.size(100);
//            //各种组合条件
//            rq.source(sourceBuilder);
//
//            //请求
//            System.out.println(rq.source().toString());
//            SearchResponse rp = client.search(rq,RequestOptions.DEFAULT);
//            System.out.println(rp);
//
////            GetRequest getRequest = new GetRequest(
////                    index,//索引
////                    type,//类型
////                    "1");//文档ID
////
////            GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
////            System.out.println(getResponse);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//
//    @RequestMapping("/put")
//    public void put() throws IOException {
//        //1.准备一个json数据
//        try {
//            for (int i = 0; i < 10000000; i++) {
//                AtcDrgStat atcDrgStat = new AtcDrgStat();
//                atcDrgStat.setAdrgCode("AdrgCode");
//                atcDrgStat.setAdrgName("AdrgName");
//                atcDrgStat.setDistrictCode("DistrictCode");
//                atcDrgStat.setDrgAmount(1000);
//                atcDrgStat.setYearMonth("yearMonth");
//                atcDrgStat.setHospitalLevel("HospitalLevel");
//                atcDrgStat.setDrgAvg(10);
//                atcDrgStat.setDrgCode("DrgCode");
//                atcDrgStat.setDrgCount(100);
//                atcDrgStat.setDrgName("DrgName");
//                atcDrgStat.setDrugCodeStd("DrugCodeStd");
//                atcDrgStat.setDrugNameStd("DrugNameStd");
//                atcDrgStat.setDrugHospitalTime(10000);
//                atcDrgStat.setSortId(i);
//                String json= objectMapper.writeValueAsString(atcDrgStat);
//                //2.准备一个request对象（手动指定id)
//                IndexRequest request=new IndexRequest(index,type,String.valueOf(atcDrgStat.getSortId()));
//                request.source(json, XContentType.JSON);
//                //3.通过client对象执行添加
//                IndexResponse resp =client.index(request, RequestOptions.DEFAULT);
//
//                //4.输出返回结果
//                System.out.println(resp.getResult().toString());
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//    }
//
//    @RequestMapping("/post")
//    public void post() throws IOException {
//        //创建Map修改指定内容
//        try {
//            Map<String,Object> doc= new HashMap<String, Object>();
//            doc.put("drg_name","张大三");
//            String docID="1";
//
//            UpdateRequest request=new UpdateRequest(index,type,docID);
//            request.doc(doc);
//            UpdateResponse update=client.update(request,RequestOptions.DEFAULT);
//            System.out.println(update.getResult().toString());
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
//
//    public   void exists() throws IOException {
//        //1.准备request对象
//        GetIndexRequest request=new GetIndexRequest();
//        request.indices(index);
//        //2.通过Cilent操作
//        boolean exists= client.indices().exists(request,RequestOptions.DEFAULT);
//        //3.输出
//        System.out.println(exists);
//    }
//
//    @RequestMapping("create")
//    public void create() throws IOException {
//        //1.索引的settings
//        Settings.Builder settings = Settings.builder()
//                .put("number_of_shards", 2)
//                .put("number_of_replicas", 1);
//
//        //2.索引的mappings
//        XContentBuilder mappings = JsonXContent.contentBuilder();
//        mappings.startObject()
//                    .startObject("properties")
//                        .startObject("district_code")
//                            .field("type", "keyword")
//                        .endObject()
//                        .startObject("year_month")
//                            .field("type", "keyword")
//                        .endObject()
//                        .startObject("hospital_level")
//                            .field("type", "keyword")
//                        .endObject()
//                        .startObject("adrg_code")
//                            .field("type", "keyword")
//                        .endObject()
//                        .startObject("adrg_name")
//                            .field("type", "keyword")
//                        .endObject()
//                        .startObject("drg_code")
//                            .field("type", "keyword")
//                        .endObject()
//                        .startObject("drg_name")
//                            .field("type", "keyword")
//                        .endObject()
//                        .startObject("drug_code_std")
//                            .field("type", "keyword")
//                        .endObject()
//                        .startObject("drug_name_std")
//                            .field("type", "keyword")
//                        .endObject()
//                        .startObject("person_count")
//                            .field("type", "integer")
//                        .endObject()
//                        .startObject("person_avg")
//                            .field("type", "integer")
//                        .endObject()
//                        .startObject("drg_count")
//                            .field("type", "integer")
//                        .endObject()
//                        .startObject("drg_avg")
//                            .field("type", "integer")
//                        .endObject()
//                        .startObject("drg_amount")
//                            .field("type", "integer")
//                        .endObject()
//                        .startObject("drug_hospital_time")
//                            .field("type", "integer")
//                        .endObject()
//                        .startObject("drug_count")
//                            .field("type", "integer")
//                        .endObject()
//                        .startObject("sort_id")
//                            .field("type", "integer")
//                        .endObject()
//                    .endObject()
//                .endObject();
//        //将settings和Mappings 封装到一个Request对象
//        String in="tempindex";
//        String doc="_doc";
//        CreateIndexRequest request = new CreateIndexRequest(in)
//                .settings(settings)
//                .mapping(doc, mappings);
//        //通过Client对象连接ES并执行创建索引
//
//        try {
//            CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
//
//            System.out.println("resp:" + createIndexResponse.toString());
//        } catch (Exception ex) {
//            System.out.println("resp:" + ex.getMessage());
//            throw ex;
//        }
//
//    }
//
//    @RequestMapping("drop")
//    public void drop() throws IOException {
//        //1.准备request对象
//        DeleteIndexRequest request=new DeleteIndexRequest();
//        request.indices(index);
//        //2.通过Cilent操作
//        AcknowledgedResponse delete=client.indices().delete(request,RequestOptions.DEFAULT);
//        /* 3.输出 */
//        System.out.println(delete);
//    }
//}
